package com.zjitc.controller;

import com.zjitc.common.ServerResponse;
import com.zjitc.model.User;
import com.zjitc.service.UserService;
import com.zjitc.utils.Conster;
import com.zjitc.utils.Utils;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/27 15:09
 * @description:
 */
@Controller
@RequestMapping("/user")
public class UserController {
  private static final Integer role = 1;

  @Autowired
  private UserService service;

  @RequestMapping(value = "/login")
  @ResponseBody
  public ServerResponse<User> login(
      @RequestParam(value = "username") String username,
      @RequestParam(value = "password") String password,
      HttpSession session
  ) {
    ServerResponse<User> response = service.login(username, password);
    if (response.success()) {
      session.setAttribute(Conster.USER, response.getData());
    }
    return response;

  }

  @RequestMapping(value = "/register")
  @ResponseBody
  public ServerResponse<User> register(User user) {
    user.setRole(role);
    return service.register(user);
  }

  @RequestMapping(value = "/check_valid")
  @ResponseBody
  public ServerResponse<User> checkValid(
      @RequestParam("str") String str,
      @RequestParam("type") String type) {
    return service.checkValid(str,type);
  }

  @RequestMapping(value = "/get_user_info")
  @ResponseBody
  public ServerResponse<User> getUserInfo(HttpSession session) {
    User user = (User) session.getAttribute(Conster.USER);
    if (user != null) {
      return ServerResponse.createSuccess(user);
    }
    return ServerResponse.createError("用户未登录,无法获取当前用户信息");
  }

  @ResponseBody
  @RequestMapping(value = "/forget_get_question")
  public ServerResponse<User> forgetPassword(String username) {
    return service.forgetPassword(username);
  }

  @RequestMapping(value = "/forget_check_answer")
  @ResponseBody
  public ServerResponse<User> forgetCheckAnswer(
      @RequestParam(value = "username") String username,
      @RequestParam(value = "question") String question,
      @RequestParam(value = "answer") String answer
  ) {
    return  service.forgetCheckAnswer(username, question, answer);
  }

  @RequestMapping(value = "/forget_reset_password")
  @ResponseBody
  public ServerResponse<User> forgetResetPassword(
      @RequestParam(value = "username") String username,
      @RequestParam(value = "passwordNew") String passwordNew,
      @RequestParam(value = "forgetToken") String forgetToken
  ) {
    return service.forgetResetPassword(username, passwordNew, forgetToken);
  }

  @ResponseBody
  @RequestMapping(value = "/update_information")
  public ServerResponse<User> updateInformation(
      @RequestParam(value = "email", required = false) String email,
      @RequestParam(value = "phone", required = false) String phone,
      @RequestParam(value = "question", required = false) String question,
      @RequestParam(value = "answer", required = false) String answer,
      HttpSession session) {
    User user = (User) session.getAttribute(Conster.USER);
    return service.updateInformation(email, phone, question, answer, user);
  }

  @RequestMapping(value = "/reset_password")
  @ResponseBody
  public ServerResponse<User> resetPassword(
      @RequestParam(value = "passwordOld") String passwordOld,
      @RequestParam(value = "passwordNew") String passwordNew,
      HttpSession session) {
    return service.resetPassword(passwordOld, passwordNew,session);
  }

  /**
   * 不重要
   * @param session
   * @return
   */
  @RequestMapping(value = "/get_information")
  @ResponseBody
  public ServerResponse<User> getInformation(HttpSession session) {
    User user = (User) session.getAttribute(Conster.USER);
    return service.getInformation(user);
  }

  @RequestMapping(value = "/logout")
  @ResponseBody
  public ServerResponse<User> logout(HttpSession session) {
    try {
      session.removeAttribute(Conster.USER);
      return ServerResponse.createSuccess("退出成功");
    } catch (Exception e) {
      return ServerResponse.createError("服务器异常");
    }
  }

}
